package com.gosophia.conference.action.confPrepare;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.conferenceManagement.common.dto.DocumentDTO;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.common.entity.DocumentInfo;
import com.gosophia.conferenceManagement.common.service.ConferenceFileService;
import com.gosophia.conferenceManagement.confPreparation.ws.ConfPreparationWebService;
import com.gosophia.oa.action.JudgeCurrentUndertaker;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.opensymphony.xwork2.ActionContext;
/**
 * 上传会议材料
 * @author wangxinbing
 * @since  2011-3-28
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Component("UploadConfMetailsAction")
@Scope("prototype")
public class UploadConfMetailsAction extends BaseSupportAction {
	
	@Autowired
	private ConfPreparationWebService confPreparationWebService; 
	@Autowired
	private ConferenceFileService conferenceFileService; 
	@Autowired
	private JudgeCurrentUndertaker judgeCurrentUndertaker;
	
	private Long jobId;
	private File[] files;
	private String[] filesFileName;
	private String[] filesNode;
	private List<DocumentDTO> materials = new ArrayList<DocumentDTO>();
	private List<DocumentDTO> hasFileLists = new ArrayList<DocumentDTO>();
	@Override
	public String _execute() throws Exception {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long operatorId = userDetails.getUserAccountDTO().getUserInfoDTO().getUserInfoId();
        //判断是否匹配
        judgeCurrentUndertaker.judgeOperationAuthority(jobId);
		if(files != null && files.length != 0){
			//将附件保存到内容管理仓库
			for(int i = 0 ; i< files.length ; i++){
				DocumentInfo docInfo = new DocumentInfo();
				Document documentBasicInfo = new Document();
				documentBasicInfo.setDocumentName(filesFileName[i]);
//				documentBasicInfo.setDocumentPath(documentPath)
				docInfo.setDocumentBasicInfo(documentBasicInfo);
				if(files[i].exists()){
					FileInputStream documentContent = new FileInputStream(files[i]);
					docInfo.setDocumentContent(documentContent);
				}
				if(filesFileName[i] !=null && !filesFileName[i].isEmpty()){
					documentBasicInfo = conferenceFileService.saveDocumentData(docInfo);
					DocumentDTO material = new DocumentDTO();
					material.setDocumentPath(documentBasicInfo.getDocumentPath());
					material.setDocumentVersion(documentBasicInfo.getDocumentVersion());
					material.setDocumentName(filesFileName[i]);
					material.setDocumentNote(filesNode[i]);
					materials.add(material);
				}
			}
		}
		if(hasFileLists!=null){
			if(materials !=null && materials.size() !=0){
				hasFileLists.addAll(materials);
			}
		}
		
		for(DocumentDTO ma:hasFileLists){
			ma.setDocumentId(null);
			ma.setDocumentCategory("attachment");
		}
		confPreparationWebService.updateConfMaterials(jobId,hasFileLists);
		super.getRequestResult().setMessage("会议材料上传成功");
		super.getRequestResult().setRsltCode("0");
		ActionContext.getContext().getSession().put("_lastOpResult",
				super.getRequestResult());
		return SUCCESS;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public File[] getFiles() {
		return files;
	}

	public void setFiles(File[] files) {
		this.files = files;
	}

	public String[] getFilesFileName() {
		return filesFileName;
	}

	public void setFilesFileName(String[] filesFileName) {
		this.filesFileName = filesFileName;
	}

	public String[] getFilesNode() {
		return filesNode;
	}

	public void setFilesNode(String[] filesNode) {
		this.filesNode = filesNode;
	}

	public List<DocumentDTO> getMaterials() {
		return materials;
	}

	public void setMaterials(List<DocumentDTO> materials) {
		this.materials = materials;
	}

	public List<DocumentDTO> getHasFileLists() {
		return hasFileLists;
	}

	public void setHasFileLists(List<DocumentDTO> hasFileLists) {
		this.hasFileLists = hasFileLists;
	}

}
